package com.zyg.controller;

import com.alibaba.fastjson2.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zyg.pojo.Student;
import com.zyg.service.StudentService;
import com.zyg.util.LayData;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
@RequestMapping("/student")
public class StudentController {

    @Autowired
    StudentService studentService;
    @RequestMapping("/tolist")
    public String tolist(){
        return "student/list";
    }
    @RequestMapping("/list")
    @ResponseBody
    public String list(Student student, @RequestParam(value="page",defaultValue = "1")Integer page, @RequestParam(value="limit",defaultValue = "10")Integer limit){
        LayData laydata = new LayData();
        QueryWrapper<Student> qw=new QueryWrapper<>();
        if(student.getStudentName()!=null&&!"".equals(student.getStudentName())){
            qw.like(("student_name"),student.getStudentName());
        }
        qw.eq("del_flag" ,0);
        long count=studentService.count(qw);
        laydata.setCount((int)count);
        Page<Student> page1=new Page<>(page,limit);
        List<Student> list=studentService.list(page1,qw);
        laydata.setData(list);
        laydata.setCode(0);
        laydata.setMsg("查询成功");
        String data= JSON.toJSONString(laydata);
        return data;
    }
    @RequestMapping("/toadd")
    public String toadd(){
        return "student/add";
    }
    @RequestMapping("/addSave")
    @ResponseBody
    public String addSave(Student student){
        boolean flag = studentService.save(student);
        if(flag){
            return JSON.toJSONString(new LayData(0,"添加成功"));
        }else{
            return JSON.toJSONString(new LayData(500,"添加失败"));
        }
    }
    @RequestMapping("/toupdate/{id}")
    public String toupdate(@PathVariable("id")Integer id, Model model){
        Student byId = studentService.getById(id);
        model.addAttribute("student",byId);
        return "student/update";
    }
    @RequestMapping("/updateSave")
    @ResponseBody
    public String updateSave(Student student){
        boolean flag = studentService.updateById(student);
        if(flag){
            return JSON.toJSONString(new LayData(0,"修改成功"));
        }else{
            return JSON.toJSONString(new LayData(500,"修改失败"));
        }
    }
    @RequestMapping("/delete/{id}")
    @ResponseBody
    public String delete(@PathVariable("id")Integer id){
        boolean flag = studentService.removeById(id);
        if(flag){
            return JSON.toJSONString(new LayData(0,"删除成功"));
        }else{
            return JSON.toJSONString(new LayData(500,"删除失败"));
        }
    }
}
